import { Switch,Route,Redirect,withRouter } from "react-router-dom";

function RouterView(props){
    const {routes} = props
    return <Switch>
        {
            routes.map((item,index)=>{
                if(item.component){
                    return <Route key={index} exact={item.exact} path={item.path} 
                    render={()=>{
                        if(item.meta.mustLogin){
                            //是否登录
                            const userinfo = JSON.parse(localStorage.getItem('login_data')||'{}');
                            if(!userinfo.token){
                                return <Route key={index} exact={item.exact} path={item.path}>
                                <Redirect to='/login'/>
                                </Route>
                            }//没有登录
                        }
                        JSON.parse(localStorage.getItem('login_data')||'{}')
                        item.meta.title && (document.title=item.meta.title)
                        const Component=withRouter(item.component)
                        return <Component {...props}/>
                }}/>
                }else{
                    return <Route key={index} exact={item.exact} path={item.path}>
                         <Redirect to={item.redirect} />       
                    </Route>
                }
            
            })
        }
    </Switch>
}

export default RouterView